package com.gxa.servlet.goods;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Result;
import com.gxa.pojo.goods.GoodsOneType;
import com.gxa.pojo.goods.GoodsTwoType;
import com.gxa.pojo.goods.Type;
import com.gxa.service.goods.GoodsOneTypeService;
import com.gxa.service.goods.GoodsTwoTypeService;
import com.gxa.service.goods.GoodsTypeService;
import com.gxa.service.goods.impl.GoodsOneTypeServiceimpl;
import com.gxa.service.goods.impl.GoodsTwoTypeServiceimpl;
import com.gxa.service.goods.impl.GoodsTypeServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/GoodsTwoTypeServlet")
public class GoodsTwoTypeServlet extends HttpServlet {
    GoodsTwoTypeService goodsTwoTypeService = new GoodsTwoTypeServiceimpl();
    GoodsTypeService goodsTypeService = new GoodsTypeServiceimpl();
    GoodsOneTypeService goodsOneTypeService = new GoodsOneTypeServiceimpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //因为request.getreader（）方法只能读一次，采样下面几行代码
//        BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
//        StringBuilder json = new StringBuilder();
//        String inputStr;
//        while ((inputStr = streamReader.readLine()) != null) {
//            json.append(inputStr);
//        }
//        String s = json.toString(); //这是将json转化为字符串的形式
//        GoodsTwoType goodsTwoType = JSON.parseObject(s, GoodsTwoType.class);
        String twotypename = request.getParameter("twotypename");
        String tid = request.getParameter("tid");
        int i = Integer.parseInt(tid);
        List<GoodsOneType> goodsOneTypes = goodsOneTypeService.select1(i);


        int inserttwo = goodsTwoTypeService.inserttwo(twotypename,i);

        GoodsOneType goodsOneType = goodsOneTypes.get(0);
//        System.out.println(onetypename);
        String onetypename = goodsOneType.getOnetypename();
        Type type = new Type();
        type.setFather(onetypename);
        type.setTypename(twotypename);
        type.setLevel("二级");
        goodsTypeService.insert(type);


        Map<String, Object> map = new HashMap<>();
        if (inserttwo!=0){
            map.put("Result", Result.ok("添加成功"));
        }else {
            map.put("Result",Result.error());
        }
        String s1 = JSON.toJSONString(map);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s1);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }
}
